package com.uyayo.framework.common.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import com.uyayo.framework.common.util.date.DateFormatExt;


public class CodeUtil {
	private static Long lastVersionTime;
	public static String[] CODE = { "a", "b", "c", "d", "e", "f", "g", "h",
			"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
			"v", "w", "x", "y", "z" };
	public static String[] CODE_BIG = { "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
			"V", "W", "X", "Y", "Z" };

	public static String getId(String id, int length) {
		String lengthTemp = "";
		for (int i = 0; i < length - 1; i++) {
			lengthTemp = lengthTemp + "0";
		}

		String str = "";
		if ((id == null) || (id.equals(""))) {
			str = lengthTemp + "1";
		} else {
			str = id;
			int tempId = Integer.parseInt(str);
			tempId++;
			str = Integer.toString(tempId);

			str = lengthTemp.substring(0, length - str.length()) + str;
		}
		return str;
	}

	public static final String getRandom(int pLength) {
		String random = "";
		for (int i = 0; i < pLength; i++) {
			int t = (int) (Math.random() * 10.0D);
			random = random + String.valueOf(t);
		}
		return random;
	}

	public static final String getTimeSequenceAsRandom(int pLength) {
		int tmpLength = pLength;

		if (tmpLength < 20) {
			tmpLength = 20;
		}
		if (tmpLength > 32) {
			tmpLength = 32;
		}

		String Sequence = "";

		Date temp = new Date();
		Sequence = DateFormatExt.dfyyyyMMddHHMMSSSSS.format(temp);
		Sequence = Sequence + getRandom(tmpLength - 17);

		return Sequence;
	}

	/**
	 * 获取时间戳加随机编码生成的id
	 * 
	 * @param pLength
	 * @return
	 */
	public static final String getTimeSequenceAsRandomCode(int pLength) {
		int tmpLength = pLength;

		if (tmpLength < 20) {
			tmpLength = 20;
		}
		if (tmpLength > 32) {
			tmpLength = 32;
		}

		String Sequence;

		Date temp = new Date();
		Sequence = DateFormatExt.dfyyyyMMddHHMMSSSSS.format(temp);
		Sequence = Sequence + getRandomCode(tmpLength - 17);

		return Sequence;
	}

	/**
	 * 获取时间戳加随机大写编码生成的id
	 * 
	 * @param pLength
	 * @return
	 */
	public static final String getTimeSequenceAsRandomBigCode(int pLength) {
		int tmpLength = pLength;

		if (tmpLength < 20) {
			tmpLength = 20;
		}
		if (tmpLength > 32) {
			tmpLength = 32;
		}

		String Sequence = "";

		Date temp = new Date();
		Sequence = DateFormatExt.dfyyyyMMddHHMMSSSSS.format(temp);
		Sequence = Sequence + getRandomBigCode(tmpLength - 17);

		return Sequence;
	}

	public static final synchronized String getTimeSequenceAsRandomCode() {
		return getTimeSequenceAsRandomCode(32);
	}

	public static final synchronized String getTimeSequenceAsRandomBigCode() {
		return getTimeSequenceAsRandomBigCode(32);
	}
	
	public static final synchronized String getTimeSequenceAsRandom() {
		return getTimeSequenceAsRandom(32);
	}

	public static synchronized String getTimeVersion() {
		if (lastVersionTime == null) {
			lastVersionTime = Long.valueOf(Long
					.parseLong(DateFormatExt.dfyyyyMMddHHMMSSSSS
							.format(new Date())));
		}
		Long nowTime = Long
				.valueOf(Long.parseLong(DateFormatExt.dfyyyyMMddHHMMSSSSS
						.format(new Date())));

		if (nowTime.longValue() <= lastVersionTime.longValue()) {
			nowTime = Long.valueOf(lastVersionTime.longValue() + 1L);
		}
		String version = nowTime + getRandom(3);
		lastVersionTime = nowTime;
		return version;
	}

	public static BigDecimal getRandom(int start, int end) {
		return new BigDecimal(start + Math.random() * end);
	}

	public static String getRandomCode(int length) {
		StringBuffer random = new StringBuffer("");
		Random r = new Random();
		int len = CODE.length;
		for (int i = 0; i < length; i++) {
			int num = r.nextInt(len);
			if (num == len)
				num = len - 1;
			else if (num < 0) {
				num = 0;
			}
			random.append(CODE[num]);
		}
		return random.toString();
	}

	public static String getRandomBigCode(int length) {
		StringBuffer random = new StringBuffer("");
		Random r = new Random();
		int len = CODE_BIG.length;
		for (int i = 0; i < length; i++) {
			int num = r.nextInt(len);
			if (num == len)
				num = len - 1;
			else if (num < 0) {
				num = 0;
			}
			random.append(CODE_BIG[num]);
		}
		return random.toString();
	}

	public static String getGUID() {
		UUID guid = UUID.randomUUID();
		return guid.toString();
	}

	public static void main(String[] args) {
		System.out.println(getTimeSequenceAsRandomCode());
	}
}